package com.ibm.pess.order;

import java.io.Serializable;

import com.ibm.json.java.JSONArray;
import com.ibm.json.java.JSONObject;
import com.ibm.pess.rest.RestClient;

public class Order implements Serializable {

	String name;
	String id;
	String _id;
	String fromLoc;
	String toLoc;
	String status;
	String shipper;
	String receiver;
	String loc;

	private static final String orderTenXUrl = "http://tenx.ng.w3.bluemix.net/collections/Order";

	public Order(final String id) {
		this.id = id;
		load();
	}

	public Order(final String name, final String fromLoc, final String toLoc,
			final String status, final String shipper, final String receiver,
			final String loc) {
		this.name = name;
		this.id = Long.toString(System.currentTimeMillis());
		this.fromLoc = fromLoc;
		this.toLoc = toLoc;
		this.status = status;
		this.receiver = receiver;
		this.shipper = shipper;
		this.loc = loc;
		create();
	}

	private void create() {
		JSONObject o = new JSONObject();
		o.put("NAME", name);
		o.put("ID", id);
		o.put("STATUS", "NEW");
		o.put("TO_LOC", toLoc);
		o.put("FROM_LOC", fromLoc);
		o.put("SHIPPER", shipper);
		o.put("RECEIVER", receiver);
		o.put("LOC", loc);
		try {
			RestClient.postJSONObject(orderTenXUrl, "", "", o);
			get_Id();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void update() {
		JSONObject o = new JSONObject();
		o.put("NAME", name);
		o.put("ID", id);
		o.put("STATUS", status);
		o.put("TO_LOC", toLoc);
		o.put("FROM_LOC", fromLoc);
		o.put("SHIPPER", shipper);
		o.put("RECEIVER", receiver);
		o.put("LOC", loc);
		try {
			RestClient.putJSONObject(
					String.format("%s/%s", orderTenXUrl, _id), "", "", o);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void get_Id() {
		JSONArray orders;
		try {
			orders = RestClient.getJSONArray(orderTenXUrl, "", "");
			for (Object x : orders) {
				String temp = (String) ((JSONObject) x).get("ID");
				if (temp.equals(id)) {
					_id = new String((String) ((JSONObject) x).get("_id"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void load() {
		JSONArray orders;
		try {
			orders = RestClient.getJSONArray(orderTenXUrl, "", "");
			for (Object x : orders) {
				String temp = (String) ((JSONObject) x).get("ID");
				if (temp.equals(id)) {
					_id = new String((String) ((JSONObject) x).get("_id"));
					name = new String((String) ((JSONObject) x).get("NAME"));
					status = new String((String) ((JSONObject) x).get("STATUS"));
					toLoc = new String((String) ((JSONObject) x).get("TO_LOC"));
					fromLoc = new String(
							(String) ((JSONObject) x).get("FROM_LOC"));
					shipper = new String(
							(String) ((JSONObject) x).get("SHIPPER"));
					receiver = new String(
							(String) ((JSONObject) x).get("RECEIVER"));
					loc = new String((String) ((JSONObject) x).get("LOC"));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getFromLoc() {
		return fromLoc;
	}

	public void setFromLoc(String fromLoc) {
		this.fromLoc = fromLoc;
	}

	public String getToLoc() {
		return toLoc;
	}

	public void setToLoc(String toLoc) {
		this.toLoc = toLoc;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getShipper() {
		return shipper;
	}

	public void setShipper(String shipper) {
		this.shipper = shipper;
	}

	public String getReceiver() {
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = receiver;
	}

	public String getLoc() {
		return loc;
	}

	public void setLoc(String loc) {
		this.loc = loc;
	}

}
